package com.dyrj.question.service.impl;

import com.dyrj.question.bean.PCtgz;
import com.dyrj.question.bean.PDayanswer;
import com.dyrj.question.bean.PTkgl;
import com.dyrj.question.dao.PCtgzMapper;
import com.dyrj.question.dao.PTkglMapper;
import com.dyrj.question.service.IPTkglService;
import org.springframework.stereotype.Service;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 题库管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Service
@Log4j2
public class PTkglServiceImpl implements IPTkglService {

    @Resource
    private PTkglMapper mapper;
    @Resource
    private PCtgzMapper pCtgzMapper;
    @Resource
    private UploadProperties uploadProperties;

    @Override
    public PageResultMsg table(PTkgl record) {
        PageInfo<PTkgl> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }


    @Override
    public ResultMsg add(PTkgl record) {
        String msg = "";
        try {
            mapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }


    @Override
    public PTkgl info(String id) {
        PTkgl ylMmdagl = mapper.selectByPrimaryKey(Integer.parseInt(id));
        return ylMmdagl;
    }

    public ResultMsg update(PTkgl record) {
        String msg = "";
        try {
            mapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
    }

    @Override
    public ResultMsg delete(String id) {
        mapper.deleteByPrimaryKey(Integer.parseInt(id));
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
    }

    /**
     * @function:从list中随机抽取若干不重复元素
     * @param paramList:被抽取list
     * @param count:抽取元素的个数
     * @return:由抽取元素组成的新list
     */
    public List getRandomList(List<PTkgl> paramList, int count){
        if(count==0){
            return null;
        }
        Random random=new Random();
        List<Integer> tempList=new ArrayList<Integer>();//临时存放产生的list索引，去除重复的索引
        List<String> tyzsdbzList=new ArrayList<String>();//临时存放产生的list索引，去除重复的索引
        List newList=new ArrayList();//生成新的list集合
        int temp=0;
        if(count<1){//如果数据小于1，取一条数据
            temp = random.nextInt(paramList.size());
            newList.add(paramList.get(temp).getQid());
        }else {
            for(int i=0;i<Math.ceil(count);i++){
                temp=random.nextInt(paramList.size());//初始化一个随机数，将产生的随机数作为被抽list的索引
                if(!tempList.contains(temp)&&!tyzsdbzList.contains(paramList.get(temp).getTyzsdbz())){//判断随机抽取的随机数
                    //把同一知识点放在tyzsdbzList集合中
                    tyzsdbzList.add(paramList.get(temp).getTyzsdbz());
                    tempList.add(temp);
                    newList.add(paramList.get(temp));
                }
                else{
                    i--;
                }
            }
        }
        return newList;
    }

    public List<PTkgl> getRandomList(String nd){
        List<PTkgl> retList = new ArrayList<>();

        PCtgz pCtgz1 = new PCtgz();
        pCtgz1.setBz(nd);
        List<PCtgz> pCtgzList = pCtgzMapper.selectList(pCtgz1);

        for(int i=0;i<pCtgzList.size();i++){
            PCtgz pCtgz = pCtgzList.get(i);
            PTkgl pTkgl = new PTkgl();
            pTkgl.setLbdm(pCtgz.getLbdm());
            List<PTkgl> tkglList1 = mapper.selectList(pTkgl);
            //随机抽取答题数
            if(tkglList1.size()>=pCtgz.getQuescn()) {
                List<PTkgl> list1 = this.getRandomList(tkglList1, pCtgz.getQuescn());
//                System.out.println(list1.size());
                for (int j = 0; j < list1.size(); j++) {
                    retList.add(list1.get(j));
                }
            }
        }
        return retList;
    }
}
